/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison.websitereader.discountmerchant;

import java.io.IOException;
import java.util.ArrayList;
import pricecomparison.PriceComparisonView;
import pricecomparison.websitereader.WebsiteReader;

/**
 *
 * @author Jim Simon
 */
public class DM_ItemReader extends DM_HomePageReader {
    String URL = super.URL;
    WebsiteReader reader;
    
    public DM_ItemReader(String url) throws java.net.MalformedURLException, java.io.IOException {
        URL = url;
        try {
            reader = new WebsiteReader(URL);
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
    }
    
    public void beginSearch() throws jxl.write.WriteException, java.io.IOException {
        String name, price;
        
        try {
            name = getItemName();
            price = getPrice();
            reader.close();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        // Write debug information
        if (reader.DEBUG) {
            System.out.println("--- Name: " + name);
            System.out.println("--- Price: " + price);
        }


        // Write item to excel
        ArrayList<String> items = new ArrayList();
        items.add(name);
        items.add(price);
        try {
            PriceComparisonView.excelWriter.writeItem(items, PriceComparisonView.excelWriter.MFER_DISCOUNTMERCHANT);
        } catch (jxl.write.WriteException e) {
            throw e;
        }
    }
    
    public String getItemName() throws java.io.IOException {
        String line;
        String key = "class=\"productnamecolor";
        
        try {
            line = reader.readLine();
            while (!line.contains(key))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        key = "\">";
        // Find beginning portion
        int pos = line.indexOf(key);
        line = line.substring(pos + key.length());
        
        // Find end portion
        key = "</font>";
        pos = line.indexOf(key);
        line = line.substring(0, pos);    
        
        return line;
    }
    
    public String getPrice() throws java.io.IOException {
        String line;
        String key = "Our Price";
        
        try {
            line = reader.readLine();
        
            while (!line.contains(key))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        line = line.substring(line.indexOf(key));
        
        // Find beginning portion
        key = "$";
        int pos = line.indexOf(key);
        line = line.substring(pos);   
        
        // Find end portion
        key = "</font>";
        pos = line.indexOf(key);
        line = line.substring(0, pos-1);
        
        return line;
    }
}
